<?php

namespace App\Services;

use Auth;
use App\Handlers\ImageUploadHandler;
use App\Repositories\AgentRepository;
use Illuminate\Support\Facades\Hash;


class AgentService
{
	protected $uploader;
	
	protected $actionLogsService;
	
	/**
	 * AdminsService constructor.
	 * @param AdminsRepository $adminsRepository
	 * @param ImageUploadHandler $imageUploadHandler
	 * @param ActionLogsService $actionLogsService
	 */
// 	public function __construct(AgentRepository $agentRepository, ImageUploadHandler $imageUploadHandler,ActionLogsService $actionLogsService)
// 	{
// 		$this->uploader = $imageUploadHandler;
// 		$this->actionLogsService = $actionLogsService;
// 	}
	
	/**
	 * 创建管理员数据
	 * @param $request
	 * @return mixed
	 */
	public function create($request)
	{
		$datas = $request->all();
		//print_r($request->all());exit;
		//上传头像
// 		if ($request->avatr) {
// 			$result = $this->uploader->save($request->avatr, 'avatrs');
// 			if ($result) {
// 				$datas['avatr'] = $result['path'];
// 			}
// 		}
		
// 		$datas['password'] = Hash::make($request->password);
// 		$datas['create_ip'] = $request->ip();
// 		$datas['last_login_ip'] = $request->ip();
		
 		$admin = $this->agentRepository->create($datas);
		
// 		//插入模型关联数据
// 		$admin->roles()->attach($request->role_id);
		
		return $datas;
	}
	
	/**
	 * 更新管理员资料
	 * @param $request
	 * @param $id
	 * @return mixed
	 */
	public function update($request, $id)
	{
		$datas = $request->all();
		
		$admin = $this->adminsRepository->ById($id);
		
		//上传头像
		if ($request->avatr) {
			$result = $this->uploader->save($request->avatr, 'avatrs');
			if ($result) {
				$datas['avatr'] = $result['path'];
			}
		}
		
		if (isset($datas['password'])) {
			$datas['password'] = Hash::make($request->password);
		} else {
			unset($datas['password']);
		}
		
		$admin->update($datas);
		
		//更新关联表数据
		$admin->roles()->sync($request->role_id);
		
		return $admin;
	}
	
	/**
	 * 获取管理员的详细资料
	 * @param $id
	 * @return mixed
	 */
	public function ById($id)
	{
		return $this->adminsRepository->ById($id);
	}
	
	/**
	 * 获取管理员列表 with ('roles')
	 * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
	 */
	public function getAdminsWithRoles()
	{
		return $this->adminsRepository->getAdminsWithRoles();
	}
	
	
	/**
	 * 登录管理员
	 * @param $request
	 * @return bool
	 */
	public function login($request)
	{
		if(!Auth::guard('admin')->attempt([
				'name'     => $request->name,
				'password' => $request->password,
				'status'   => 1,
		])){
			//记录登录操作记录
			$this->actionLogsService->loginActionLogCreate($request,false);
			return false;
		}
		
		//增加登录次数.
		$admin = Auth::guard('admin')->user();
		
		$admin->increment('login_count');
		
		//记录登录操作记录
		$this->actionLogsService->loginActionLogCreate($request,true);
		
		return true;
	}
	
	/**
	 * 退出登录
	 * @return mixed
	 */
	public function logout()
	{
		return Auth::guard('admin')->logout();
	}
}